<?php
/**
 *图片控制类
 */
class pic extends ActionBase {
	
	private $date = null;
	
	function init() {
		$this->date['alert']['str']='';
		$this->date['alert']['color']='';
		
		if(isset($_REQUEST['issuc'])){
			$this->date['alert']['str']='操作成功！';
			$this->date['alert']['color']='green';
		}
		
		if(isset($_REQUEST['iserror'])){
			$this->date['alert']['str']='未知错误！';
			$this->date['alert']['color']='red';
		}
		
		if(isset($_REQUEST['isbig'])){
		
			$this->date['alert']['str']='图片尺寸超过2M！！';
			$this->date['alert']['color']='red';
		}
		
	}
	
	/**
	 * 动作
	 *
	 * @see AActionBase::action()
	 */
	public function action() {
		$this->date ['point'] = load_model ( 'point' )->getPoint ( $this->date ['page'] );
		
		$this->date ['count'] = load_model ( 'point' )->getRcount ( 'view_point' );
		
		$this->date ['pagecontent'] = fenye ( url ( 'point/action' ) . "&page=", $this->date ['page'] + 1, $this->date ['count'], PERPAGE );
		
		$this->template ();
		$this->template->assign ( $this->date );
		$this->template->display ( 'point.html' );
	}
	
	#路书添加图片
	public function roadbook(){
		$this->date['title'] = '路书';
		$this->date['form'] = 'roadbook';
		$this->picupload();
	}
	
	#途径地添加图片
	public function way(){
		$this->date['title'] = '途径地';
		$this->date['form'] = 'way';
		$this->picupload();
		
	}
	
	#兴趣点添加图片
	public function point(){
		$this->date['title'] = '景点';
		$this->date['form'] = 'point';
		$this->picupload();
	}


	public function picupload(){
		if(isset($_FILES['pic'])&& $_REQUEST['id']){
			$this->date['id'] = $_REQUEST['id'];
				
			$picid = load_model('pic')->replaceTable(array('type'=>($this->date['form']),'id'=>($this->date['id'])));
			if($picid){
				$upload = load_libra('uploadimg');
					
				$diypath = DATA_ROOT.'img'.iconPathBuild($picid);
				// 建目录，并赋0777权限
				if (! is_dir ( $diypath )) {
					mkdir ( $diypath, 0777, 1 );
				}
				$upload->setSrcPath($diypath);
				$result = $upload->upload($_FILES['pic'],1);
				if($result == 1){
					$filename = '/Data/img'.iconPathBuild($picid).'/'.($upload->getFileName());
					$picid = load_model('pic')->replaceTable(array('pic_id'=>$picid,'type'=>($this->date['form']),'id'=>($this->date['id']),'url'=>$filename));
			
					#$URL = str_replace('&issuc=1', '', $_SERVER ['HTTP_REFERER']);
					
					$URL = url('pic/'.($this->date['form']),array(($this->date['form']).'_id'=>($this->date['id'])))."&issuc=1";
				
				}else{
					load_model('pic')->delTable(array('pic_id'=>$picid));
					
					if($result == -4){
						$URL = url('pic/'.($this->date['form']),array(($this->date['form']).'_id'=>($this->date['id'])))."&isbig=1";
					}else{
						$URL = url('pic/'.($this->date['form']),array(($this->date['form']).'_id'=>($this->date['id'])))."&iserror=1";
					}
				}
					header ( "Location: $URL" );exit;
					
			}
		}else{
			$this->date['id'] = $_REQUEST[($this->date['form']).'_id'];
		}
		$this->date['pics'] = load_model('pic')->getTable(array('type'=>($this->date['form']),'id'=>($this->date['id'])),100);	$this->template ();
		$this->template->assign ( $this->date );
		$this->template->display ( 'pic_view.html' );
	}
	
	#删除图片
	public function del(){
		$picid=$_REQUEST['pic_id'];
	
		$pics = load_model('pic')->getTable(array('pic_id'=>$picid));
	
		load_model('pic')->delTable(array('pic_id'=>$picid));
		$picurl = '.'.($pics[0]['url']);
	
		@unlink($picurl);
		$URL = url('pic/'.$pics[0]['type'],array($pics[0]['type']."_id"=>$pics[0]['id']));
		header ( "Location: $URL" );
		exit;
	}

}
